package com.example.xcspringboot_one.service.impl;

import com.example.xcspringboot_one.mapper.BookMapper;
import com.example.xcspringboot_one.pojo.Book;
import com.example.xcspringboot_one.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

/**
 * @author xiaoChen
 * @description: AUTO GENERATION
 * @date 2022/4/24 12:07
 */
@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;


    /**
     * @param book
     * @description:
     * @return: java.lang.String
     * @author: xiaochen
     * @time: 2022/4/24 12:07
     */
    @Override

    public String save(Book book) {
        if (bookMapper.save(book) != null) {
            return "success";
        } else {
            return "error";
        }
    }

    /**
     * @param id
     * @description:
     * @return: void
     * @author: xiaochen
     * @time: 2022/4/24 12:08
     */

    @Override
    public void deleteById(Integer id) {
        bookMapper.deleteById(id);
    }

    /**
     * @param book
     * @description:
     * @return: java.lang.String
     * @author: xiaochen
     * @time: 2022/4/24 12:08
     */

    @Override
    public String update(Book book) {
        if (bookMapper.save(book) != null) {
            return "success";
        } else {
            return "error";
        }
    }

    /**
     * @param id
     * @description:
     * @return: com.example.xcspringboot_one.pojo.Book
     * @author: xiaochen
     * @time: 2022/4/24 12:08
     */

    @Override
    public Book findById(Integer id) {
        return bookMapper.findById(id).orElse(null);
    }

    /**
     * @param page
     * @param size
     * @description:
     * @return: org.springframework.data.domain.Page
     * @author: xiaochen
     * @time: 2022/4/24 12:08
     */

    @Override
    public Page<Book> findAll(Integer page, Integer size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return bookMapper.findAll(pageable);
    }


}
